package com.em.controller;

import com.em.common.DateUtils;
import com.em.entity.Resident;
import com.em.entity.User;
import com.em.service.ResidentService;
import com.em.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/resident")
public class ResidentController {
    private static String PAGE = "/resident";
    private static String REDIRECT_PAGE = "redirect:" + PAGE;

    @Autowired
    private ResidentService residentService;

    @GetMapping("/list")
    public String list(@RequestParam(value = "name", defaultValue = "") String name,
                       @RequestParam(value = "page", defaultValue = "1") int pageNumber,
                       Model model) {
        int pageSize = 10;
        List<Resident> list = residentService.select(name);
        int totalSize = list.size();
        int start = (pageNumber - 1) * pageSize;
        if (start > totalSize) {
            list = Collections.emptyList();
        } else {
            // 获取分页数据
            list = list.subList(start, Math.min(start + pageSize, totalSize));
        }
        model.addAttribute("list", list);
        model.addAttribute("pageNumber", pageNumber);
        model.addAttribute("totalPages", list.size());
        return PAGE + "/list";
    }

    @PostMapping("/add")
    public String add(@RequestParam String name,
                      @RequestParam String gender,
                      @RequestParam String phone,
                      @RequestParam String email,
                      @RequestParam String idCard,
                      @RequestParam String address,
                      @RequestParam String moveInDate,
                      @RequestParam(required = false) String moveOutDate,
                      @RequestParam String status) {
        Resident resident = new Resident();
        resident.setName(name);
        resident.setGender(gender);
        resident.setPhone(phone);
        resident.setEmail(email);
        resident.setIdCard(idCard);
        resident.setAddress(address);
        resident.setMoveInDate(moveInDate);
        resident.setMoveOutDate(moveOutDate);
        resident.setStatus(status);
        residentService.create(resident);
        return REDIRECT_PAGE + "/list";
    }

    @GetMapping("/get/{id}")
    @ResponseBody
    public Resident getResident(@PathVariable Integer id) {
        Resident resident = residentService.selectById(id);
        return resident;
    }

    @PostMapping("/update")
    public String update(@RequestParam Integer id,
                       @RequestParam(required = false) String name,
                       @RequestParam(required = false) String gender,
                       @RequestParam(required = false) String phone,
                       @RequestParam(required = false) String email,
                       @RequestParam(required = false) String idCard,
                       @RequestParam(required = false) String address,
                       @RequestParam(required = false) String moveInDate,
                       @RequestParam(required = false) String moveOutDate,
                       @RequestParam(required = false) String status) {
        Resident resident = new Resident();
        resident.setId(id);
        resident.setName(name);
        resident.setGender(gender);
        resident.setPhone(phone);
        resident.setEmail(email);
        resident.setIdCard(idCard);
        resident.setAddress(address);
        resident.setMoveInDate(moveInDate);
        resident.setMoveOutDate(moveOutDate);
        resident.setStatus(status);
        residentService.update(resident);
        return REDIRECT_PAGE + "/list";
    }


    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        residentService.delete(id);
        return REDIRECT_PAGE + "/list";
    }
}
